package com.example.myapplication3;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication3.bean.Beijing;

import java.util.ArrayList;

public class Adapter2 extends RecyclerView.Adapter {
    private ArrayList<Beijing.ResultBean> resultBeans;
    private Context context;

    public Adapter2(ArrayList<Beijing.ResultBean> resultBeans, Context context) {
        this.resultBeans = resultBeans;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.ii, viewGroup, false);
        ViewHolder holder = new ViewHolder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ViewHolder holder= (ViewHolder) viewHolder;
        Beijing.ResultBean bean = resultBeans.get(i);
        holder.t11.setText(bean.getCity());
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onClickListener!=null){
                    onClickListener.ClickListener(i);
                }
            }
        });

    }

    public interface OnClickListener{
        public void ClickListener(int position);
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public OnClickListener onClickListener;

    @Override
    public int getItemCount() {
        return resultBeans.size();
    }

    public static
    class ViewHolder extends Adapter.ViewHolder {
        public View rootView;
        public TextView t11;

        public ViewHolder(View rootView) {
            super(rootView);
            this.rootView = rootView;
            this.t11 = (TextView) rootView.findViewById(R.id.t11);
        }

    }
}
